﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Markup;
using System.Xml;

namespace editor
{
    /// <summary>
    /// Логика взаимодействия для life_time.xaml
    /// </summary>
    public partial class life_time : UserControl
    {
       public bool current;
        //время жизни
       public Rectangle _reсt;

       public bool drop_rect;
        //перетаскивание
       public bool dro=true;
        Size s;
        Brush cvet;
        public Line line_;
        Point p;
        //декоратор
        AdornerLayer layer;
        //номер в канвасе
        public int n_child;
        // есть декоратор 
        private bool pont_adorner;
        private SimpleCircleAdorner adorner;
        public bool Remove;
        private Point po_;

        public life_time()
        {
            InitializeComponent();
          
          
        }
        private void Line_MouseEnter(object sender, MouseEventArgs e)
        {
           
            Line  line = sender as Line;
            line_ = line;
            cvet = line.Stroke;
            line.Stroke = Brushes.Silver;
            if (pont_adorner == false)
            {
                layer = AdornerLayer.GetAdornerLayer((Line)sender);
                adorner = new SimpleCircleAdorner((Line)sender);
                layer.Add(adorner);
                pont_adorner = true;
            }
           
        }

        private void Line_MouseLeave(object sender, MouseEventArgs e)
        {
            Line line = sender as Line;

  
            line.Stroke = cvet;
            if (pont_adorner == true)
            {
                layer.Remove(adorner);
                pont_adorner = false;

            }

        }

        private void Line_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (dro == true)
            {
                Line line = sender as Line;
                if (line != null)
                { Mouse.Capture((Line)sender); }
            }
            else
            {
                Line line = sender as Line;
                if (line != null)
                {
                    //Rectangle rest2 = new Rectangle();
                    //rest2.Fill = Brushes.Firebrick;
                    //rest2.Stroke = Brushes.Gainsboro;
                    //rest2.Width = 50;
                    //rest2.Height = 100;

                    //Canvas.SetTop(rest2, e.GetPosition(canvasfgh).Y);
                    //Canvas.SetLeft(rest2, e.GetPosition(canvasfgh).X - 10);
                    ////добавляем элементы 
                    //canvasfgh.Children.Add(rest2);
                }
               
            }

        }

        private void Line_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (dro == true)
            {
                Line line = sender as Line;
                if (line != null)
                {
                    Mouse.Capture((Line)sender);
                    Mouse.Capture(null);
                    //e.GetPosition((Line)sender).Y координата относительно начала линии

                    Double legth_AS = Math.Abs(e.GetPosition((Line)sender).Y - ((Line)sender).Y1);
                    if (legth_AS > 40)
                    {
                        ((Line)sender).Y2 = Math.Abs(e.GetPosition((Line)sender).Y - ((Line)sender).Y1);
                    }
                    dro = false;
                }
            }
        }

        private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (Remove == true)
            {

            }
        }

        private void TextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void UserControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }
        ///////////////////////////////////////////////////////////////////////////////
        //------------------------------рект-----------------------------------------//
        ///////////////////////////////////////////////////////////////////////////////
        //инилизация
        public void new_Rect(Double Height)
        {
            
            _reсt = new Rectangle();
            //-----------------рисуем  рект
            _reсt.Fill = Brushes.White;
            _reсt.StrokeThickness = 3;
            _reсt.Stroke = Brushes.Black;
            _reсt.Width = 20;
            if (Height == 0)
            {
                _reсt.Height = 50;
            }
            else
            {
                _reсt.Height = Height;
            }
            

          
            _reсt.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(_reсt_PreviewMouseLeftButtonDown);
            _reсt.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(_reсt_PreviewMouseLeftButtonUp);

        }

      
        public void add_rect_canvac(Double X,Double Y)
        {
           //относительно линнии вставляем ректангл

            Double pos_X_0 = _line.X1;

            Canvas.SetLeft(_reсt, pos_X_0-10);
            //добавляем координаты второго рект
            Canvas.SetTop(_reсt, Y);
            canvasfgh.Children.Add(_reсt);
        }
        //нажатие  клавиши
        void _reсt_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          
            if (drop_rect == false)
            {
                drop_rect = true;

                Rectangle R = sender as Rectangle;
                if (R != null)
                {
                    Mouse.Capture((Rectangle)sender);

                    po_.Y =   Canvas.GetTop((Rectangle)sender);

                }
            }
         
            
           
        }
        void _reсt_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            if(drop_rect==true)
            {
                Rectangle R = sender as Rectangle;
                if (R != null)
                {
                    Mouse.Capture((Rectangle)sender);
                    Mouse.Capture(null);
            
                   Double hegth_AS = Math.Abs(e.GetPosition(canvasfgh).Y - po_.Y);
                    if(hegth_AS>20)
                    {
                         ((Rectangle)sender).Height =hegth_AS;
                    }
              
                }
                drop_rect = false;
            }
        }


        ///////////////////////////////////////////////////////////////////////////////
        //------------------------------рект-----------------------------------------//
        ///////////////////////////////////////////////////////////////////////////////
        public void savw_(string name_File)
        {

            FileStream fs = new FileStream(name_File, FileMode.Create);
            XamlWriter.Save(this.canvasfgh, fs);
            fs.Close();
        }


        public void loa_(string name_File)
        {
            FileStream fs = new FileStream(name_File, FileMode.Open, FileAccess.Read);
            XmlReader xmlReader = XmlReader.Create(fs);

           
           // Canvas l_t = XamlReader.Load(xmlReader) as Canvas;
            this.canvasfgh =(Canvas) XamlReader.Load(xmlReader) ;
            fs.Close();          
        }

        private void _line_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double lenth_li= _line.Y2-_line.Y1;
            double W_CHH,SUM=0;
            double h0=20;

            LinearGradientBrush myHorizontalGradient =
              new LinearGradientBrush();
            myHorizontalGradient.StartPoint = new Point(0.5, 1);
            myHorizontalGradient.EndPoint = new Point(0.5, 0);
            W_CHH = (h0 / lenth_li)/3 ;
            for (int i = 0; i < lenth_li / h0; i++)
            {

                SUM += 2*W_CHH;

                myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Black, SUM));
                myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.White, SUM));
                SUM += W_CHH;
                myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.White, SUM));
                myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Black, SUM));
            }
            _line.Stroke = myHorizontalGradient;
           
        }






    }
  
}
